package com.gxa.meiye.portal.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @Author: zy
 * @CreateTime: 2022-07-23  20:54
 */
@Component
@Data
public class PayUtil {

        private static final String url="https://openapi.alipaydev.com/gateway.do";
        private static final String APP_ID="2021000121622806";
        private static final String APP_PRIVATE_KEY="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCsjbnqOhwK/2LuoEvPxeZnaRf2FyJO2gFUqoC4FSBKL8arsuwTqNQ8y74L5BXObr9RxNuEtaFQ22PErlJON323LZuZ8Vv7kYzKx4VQ3qIsmUMkpY7fjCCArKkap3YGG92ntcHkKBrt1Yj5Z7iB+LJudUE47dYYe65/U8hT3pdjDXVuu+vVJO/kVF7BiMRQGJMJOonbNAvH6zDJZwdnFi4i9LsEfYK98RgeFpicU90JcRvI6wsBYtinGUR4BQsmwf4UfE8dHmdP6+pAqhuGxR2mesaXZbPnHjpimzwjhTV5ATzGIWmF/5g2TvlY5dae1LIng+igoRkQY6rLyIZegqSpAgMBAAECggEBAItblxgNQFK9eWghe7jdKry/ysNMYb/vH34Cihyhq0dfGIPY6ALkqixDnEQ8PERReyCN7YBbtGH6HbRd2UkkVdrX8cA9hr1BCsVGqGvssmt0iB6TuzTeFe32BEjrkE1UugpVp7cdkMoeWBadZw1JDspY1okkxHFXilF5F+RsdDVg92d1B1qc0XMgUGxFowTokbhqZEwj/3/1XU3mfujvNnLvUV0HyQw9/KtYlTHxONeOoxUgXrPOFdzDqjm+pXgIGWaOdJD+kLaZ3mtGRmIY7JogtZku/wFJwgTdGg0CBl3UudeYMn0F96o9lqQUE41hvRQak75P9auaypBiWCMTTNUCgYEA5e7s1ImtKIIkRlxkPOKoTygQIhRttB3Dbmp8MxNyyZVdIl4rCPaq8I5lSzZy321GKRGiTqL6eU81VT6D4F9hGgyetvnhxaKLu2r0PbM/lfHHCm6GXQAAzMgVpUIbOEjIQu/FgJaRO6FPXDwrvPOvakevW74kOATOeKd854iwCa8CgYEAwB2KT0VzQtb646h2x6zW7hizITO6nYGhKIoNOowwH5sNwm1Zr9GA4H/WNpI5aSx7YPlJtrHMN6ZGlA+mDiqpsK+y3Jp0ij4nWhzUjBH8Vapa3gz7N5RDlArczzZxOQ1xDwU4p7AD7Wy6J6fEnLTHY55EHRhojjR0iu63GS57RScCgYEAgvMLlg/CuitEFX5Q6Av95vbeGUqhH89DmgKzMTOGUB/spsISqEwzFkVCkw8zZOOK7aDJWxeepq/ho+aynHo1huxhB7xQRIpKIjbLWshXEkJcJSjFBPoXeokJKtD3R5rL0rNTO+SEWf2IgPMTxczSviiNSiaD4hN6s2RP0l0aRKkCgYBhgpzg/28xTcyqIKepUaEF2KOTBp6Bs3oJCYkNXnpOkHS7aqVLCBtR15DyweVB7MHyxpUTlcsBs/O+0vKF3kGi3Ir8Tak5QbLXmiqHrdlV4dGdQxl/bFGgoqLQyeFOA/SOmyfWdRCJQdZ6qjyxDVDmzz3jRrBK/XTpz6A/t8f5oQKBgDJnfJnDP+65DMNqH0peY6ICNAl5ppllNM9TOO6iBHwRziIDQAoQd8FaXCdrji7WcCT70bcAprZ1+iUrDa776hT6E6dTDFi96ZgEQ9UzFCM6fB8JaSrePOoeUHuldFYCA7v53fJFwkr+17yCp3v4T+GMSI2E7+MBx7IHdEOKyKDp";
        private static final String FORMAT="JSON";
        private static final String CHARSET="GBK";
        private static final String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAreu6hfJs2p8Emun33hAeCEtfGmmPpqVeUjHByX3H/NJttGPM+5tTPWc/xAS6NwicUyb2hhmgbetjysSl6fBW32ZaMq1IGY+rSaN1kCdBR5t98fFNMeiS3iT57z/wIO971RUqQRBgQnTDok7VXqI6FtBAsjrl+a5rz2yyvCyHAHIpUDaCiudfi+yYHNRcMZljdl+I/bEDz1GzyE6+19JxKy2zgfWCI1vhvqsTbqTPfGXd3B9qPEePdco29+UdCRTAvZgJl440l6TdpiJJ1cCSoBX0tNRUeHqbbgkbWEGF15nP5iQzjyJVDdxRjKt8Zk4DxgiXlWybqL3iEcM3lOFBFwIDAQAB";
        private static final String SIGN_TYPE="RSA2";
        private  String out_trade_no=null;

        public String pay(String serialNumber,String money) throws JsonProcessingException {

            //String s = UUID.randomUUID().toString().replaceAll("-","");
            //String json= JSON.toJSONString(s);
            ObjectMapper objectMapper=new ObjectMapper();
            String s1 = objectMapper.writeValueAsString(serialNumber);
            //out_trade_no=s1;
            System.out.println(s1);
            AlipayClient alipayClient = new DefaultAlipayClient(url, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);  //获得初始化的AlipayClient
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); //创建API对应的request
            //alipayRequest.setReturnUrl("http://localhost:8081/payReturn");
            alipayRequest.setNotifyUrl("http://172.16.142.53:8081/payReturn"); //在公共参数中设置回跳和通知地址
            alipayRequest.setBizContent("{" +
                    "    \"out_trade_no\":" +s1+","+
                    "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                    "    \"total_amount\":" +money+"," +
                    "    \"subject\":\"Iphone6 16G\"," +
                    "    \"body\":\"Iphone6 16G\"," +
                    "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
                    "    \"extend_params\":{" +
                    "    \"sys_service_provider_id\":\"2088511833207846\"" +
                    "    }" +
                    "  }"); //填充业务参数
            String form = "";
            try {
                form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
            } catch (AlipayApiException e) {
                e.printStackTrace();

            }
            return form;
        }
    public String send(String number,String money){
        AlipayTradeRefundResponse response=null;
        String returnStr=null;
        String s2=number;
        System.out.println(s2);
        try {


            AlipayClient alipayClient = new DefaultAlipayClient(url, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,"RSA2" );
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            request.setBizContent("{" +
                    "    \"out_trade_no\":"+s2+"," +
//                    "    \"trade_no\":\"2014112611001004680073956707\"," +
                    "    \"refund_amount\":" +money+"," +
                    "    \"refund_reason\":\"正常退款\"," +
                    "    \"out_request_no\":\"HZ01RF001\"," +
                    "    \"operator_id\":\"OP001\"," +
                    "    \"store_id\":\"NJ_S_001\"," +
                    "    \"terminal_id\":\"NJ_T_001\"" +
                    "  }");
            response = alipayClient.execute(request);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
            returnStr="退款成功";
        } else {
            System.out.println("调用失败");
            returnStr = response.getSubMsg();//失败会返回错误信息
        }
        return returnStr;
    }
}
